package com.nsoft.html5npub;

import com.nsoft.html5npub.R;
import com.nsoft.html5npub.SettingActivity;
import com.nsoft.html5npub.business.PreferenceLoader;
import com.nsoft.html5npub.business.ServerGridSyncEngine;
import com.nsoft.html5npub.business.ServerSyncEngine;
import com.nsoft.html5npub.business.SyncBookThread;
import com.nsoft.html5npub.business.dbengine;
import com.nsoft.html5npub.objects.Settings;
import com.nsoft.html5npub.objects.Settings.InterfaceType;
import com.nsoft.html5npub.utils.Constant;
import com.nsoft.html5npub.utils.CoverFlow;
import com.nsoft.html5npub.utils.utils;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;

@SuppressWarnings("deprecation")
public class Html5NPubActivity extends FragmentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		/**
    	 * loading settings
    	 */
    	PreferenceLoader.loadSettings(this);
    	startDb();
    	
    	if (Settings.getInterfaceType()==InterfaceType.VerticalList){
    		setContentView(R.layout.activity_html5_npub);
    		startUp();
    	}else if (Settings.getInterfaceType()==
    						InterfaceType.GridList){
    		setContentView(R.layout.activity_html5_npub_grid);
    		startGridUp();
    	}
		
	}
	
	@Override
    public void onBackPressed() {
		confirmExit();
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.main_option_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()){
	    	case R.id.itemSyncServer:{
	    		
	    		break;
	    	}
	    	case R.id.itemAbout:{
	    		
    			break;
	    	}
    	}
    	
    	return super.onOptionsItemSelected(item);
    }
	
	/**
	 * takes care of controls in grid mode
	 */
	private void startGridUp(){
		Gallery gridGallery=(Gallery)
				findViewById(R.id.gridGallery);
		
		View scaleView=(View)findViewById(
					R.id.topScaleView);
		
		int[] gridSize=utils.getGridSizeLayout(this);
		
		scaleView.setLayoutParams(new LinearLayout.
				LayoutParams(LinearLayout.LayoutParams.
						FILL_PARENT,gridSize[0]));
		
		ImageButton refreshBtn=(ImageButton)
				findViewById(R.id.refreshButton);
    	refreshBtn.setOnClickListener(new 
    			ServerGridSyncEngine(this,gridGallery));
    	
		ImageButton settingBtn=(ImageButton)
				findViewById(R.id.settingButton);
		settingBtn.setOnClickListener(new 
			View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent=new Intent(
							Html5NPubActivity.this,
							SettingActivity.class);
					Html5NPubActivity.this.
						startActivity(intent);
				}
			});
		
    	ImageButton exitBtn=(ImageButton)
    			findViewById(R.id.exitButton);
    	exitBtn.setOnClickListener(new 
    				View.OnClickListener() {
			@Override
			public void onClick(View v) {
				confirmExit();
			}
		});
    	
    	/**
    	 * loading book list
    	 */
		new ServerGridSyncEngine(this,gridGallery).
			getBookList(SyncBookThread.SYNC_SOURCE_HISTORY);
	}
	
	/**
	 * takes care of controls in vertical list mode
	 */
	private void startUp(){
		/**
		 * adding controls
		 */
    	CoverFlow newList=(CoverFlow)
				findViewById(R.id.newBookList);
		int scrH=utils.getDeviceMetrics(this).heightPixels;
		newList.setLayoutParams(new 
			LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT,(int)scrH/3));
		newList.setSpacing(-25);
		newList.setAnimationDuration(1000);

		ListView list=(ListView)
				findViewById(R.id.bookListView);
		
		ImageButton refreshBtn=(ImageButton)
				findViewById(R.id.refreshButton);
    	refreshBtn.setOnClickListener(
    			new ServerSyncEngine(this,list,newList));
	    
		ImageButton settingBtn=(ImageButton)
				findViewById(R.id.settingButton);
		settingBtn.setOnClickListener(new 
			View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent=new Intent(
							Html5NPubActivity.this,
							SettingActivity.class);
					Html5NPubActivity.this.
						startActivity(intent);
				}
			});
		
    	ImageButton exitBtn=(ImageButton)
    			findViewById(R.id.exitButton);
    	exitBtn.setOnClickListener(new 
    				View.OnClickListener() {
			@Override
			public void onClick(View v) {
				confirmExit();
			}
		});
		
    	/**
    	 * loading book list
    	 */
		new ServerSyncEngine(this,list,newList).
			getBookList(SyncBookThread.SYNC_SOURCE_HISTORY);
	}
	
	private void startDb(){
		dbengine.startDb(this);
	}
	
	private void confirmExit(){
		AlertDialog.Builder alert=
				new AlertDialog.Builder(
						Html5NPubActivity.this);
			alert.setIcon(R.drawable.ic_help);
			alert.setTitle(Constant.DIALOG_CONFIRM).
				setMessage(Constant.
						booklist.BLA_CFM_QUIT).
				setPositiveButton(Constant.DIALOG_YES,
					new DialogInterface.OnClickListener(){
						@Override
						public void onClick(
								DialogInterface arg0, 
								int arg1){
							finish();
						}
					
				}).setNegativeButton(Constant.DIALOG_NO,
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(
								DialogInterface dialog,
								int which){
							dialog.cancel();
						}
			}).create().show();
	}
}
